package com.arcaneshift.servlet.action.game;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.arcaneshift.model.action.game.Player;
import com.arcaneshift.model.request.GetQueuePlayerGameRequest;
import com.arcaneshift.model.response.GetQueuePlayerGameResponse;
import com.arcaneshift.model.validators.QueuePlayerValidator;
import com.arcaneshift.model.validators.system.SessionValidator;
import com.arcaneshift.servlet.util.network.RequestAction;
import com.arcaneshift.servlet.util.parser.GetQueuePlayerGameXMLParser;
import com.arcaneshift.util.PlayerCacheContainer;
import com.arcaneshift.util.SessionCacheContainer;
import com.common.model.abstracts.AbstractRequest;
import com.common.model.abstracts.AbstractValidator;
import com.common.model.validators.ActionValidator;
import com.common.security.errors.ParserError;
import com.common.security.exceptions.ParserException;
import com.common.servlet.abstracts.AbstractParserServlet;
import com.common.servlet.interfaces.IParser;

public class GetQueuePlayerGameServlet extends AbstractParserServlet {
	private static final long serialVersionUID = 1L;
	private static Set<AbstractValidator> validators = new HashSet<AbstractValidator>();

	private GetQueuePlayerGameRequest requestContent;
	private GetQueuePlayerGameXMLParser parser = new GetQueuePlayerGameXMLParser();

	static {
		validators.add(new ActionValidator(RequestAction.GET_QUEUE_PLAYER_GAME));
		validators.add(new SessionValidator());
		validators.add(new QueuePlayerValidator());
	}

	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response) {
		try {
			GetQueuePlayerGameResponse getQueuePlayerGameResponse = new GetQueuePlayerGameResponse();
			String sessionId = requestContent.getSessionId();
			Player player = PlayerCacheContainer.getPlayer(sessionId);
			String gameId = player.getGameId();

			if (player.isInGame()) {
				removePlayer(player);
				getQueuePlayerGameResponse.setChannel(player.getChannel());
			}

			getQueuePlayerGameResponse.setRequestAction(requestContent.getAction());
			getQueuePlayerGameResponse.setGameId(gameId);
			getQueuePlayerGameResponse.setPlayerNumber(player.getPlayerNumber());

			response.setContentType("text/xml");
			response.getWriter().write(parser().parseResponse(getQueuePlayerGameResponse));
			response.getWriter().close();
		} catch (IOException ioException) {
			error(new ParserError(ParserError.STREAM_ERROR), response);
		} catch (ParserException parserException) {
			exception(parserException, response);
		}
	}

	@Override
	public void setRequest(AbstractRequest abstractRequest) {
		requestContent = (GetQueuePlayerGameRequest) abstractRequest;
	}

	@Override
	public Set<AbstractValidator> getValidators() {
		return validators;
	}

	@Override
	public IParser parser() {
		return parser;
	}
	
	private void removePlayer(Player player) {
		PlayerCacheContainer.deletePlayer(player);
		SessionCacheContainer.deleteSession(player.getSession());
	}
}
